<?php
class App_Image  {
    public $uploadPath = '/images/product/';
    protected $_fullPath;
    public $prefixs  = array('s' => array(170, 120), 'm'=>array(360, 170), 'l'=> array(1024, 768));
    
    public $file;
    
    public function __construct()
    {
        $this->_fullPath = $_SERVER['DOCUMENT_ROOT'] . $this->uploadPath;
    }
    
    public function setUploadPath($path)
    {
        $this->uploadPath = $path;
        $this->_fullPath = $_SERVER['DOCUMENT_ROOT']. $this->uploadPath;
        return $this->uploadPath;
    }
    
    public function setPrefixs($prefixs)
    {
        $this->prefixs = $prefixs;
        return  $this->prefixs;
    }
    
    public function upload()
    {
         if($_FILES){
            $uploadfile =  time() . '.jpeg';
            $this->file = $this->_fullPath . $uploadfile;
            if($_FILES['file']['type'] == 'image/jpeg'){
                if (move_uploaded_file($_FILES['file']['tmp_name'], $this->_fullPath . $uploadfile)) {
                    foreach($this->prefixs as $prefix => $size)
                    {
                    	if(!file_exists($this->_fullPath.$prefix.'/'))
                    	{
                    		mkdir($this->_fullPath.$prefix.'/');
                    	}
                        $this->resizeimg($this->_fullPath . $uploadfile, $this->_fullPath. $prefix . '/'. $uploadfile,$size[0],$size[1],95);
                    }
                    return $this->_fullPath . $uploadfile;
                }  else {
                    return false;
                }
            } else {
                return false;
            }
        }  
    }
    public  function resizeimg($image,$smallimage, $w, $h,$q)
    {
              $filename=$image;
                $size_img = getimagesize($filename);
                        if ($size_img[2]==2) {
                                $src_img = imagecreatefromjpeg($filename);  
                        }
                        else if ($size_img[2]==1)
                        $src_img = imagecreatefromgif($filename);
                        else if ($size_img[2]==3) 
                        $src_img = imagecreatefrompng($filename);
                
                
                $dest_img = imagecreatetruecolor($w, $h);
                $white = imagecolorallocate($dest_img, 255, 255, 255);
                imagefill ( $dest_img, 0, 0, $white );
                
                if($size_img[0]!=$size_img[1]){
                        if($size_img[0] > $size_img[1]){
                                $ratio =$size_img[0]/$w;
                                $t_w=$size_img[0]/$ratio;
                                $t_h=$size_img[1]/$ratio;
                                $tmp_image=imagecreatetruecolor($t_w, $t_h);
                                $y=$h/2-$t_h/2;
                                imagecopyresampled($tmp_image, $src_img, 0, 0, 0, 0, $t_w, $t_h, $size_img[0], $size_img[1]);
                                imagecopyresampled($dest_img, $tmp_image, 0, $y, 0, 0, $t_w, $t_h, $t_w,$t_h);
                        }else{
                                $ratio =$size_img[1]/$h;
                                $t_w=$size_img[0]/$ratio;
                                $t_h=$size_img[1]/$ratio;
                                $tmp_image=imagecreatetruecolor($t_w, $t_h);
                                $x=$w/2-$t_w/2;
                                imagecopyresampled($tmp_image, $src_img, 0, 0, 0, 0, $t_w, $t_h, $size_img[0], $size_img[1]);
                                imagecopyresampled($dest_img, $tmp_image, $x, 0, 0, 0, $t_w, $t_h, $t_w,$t_h);
                        }
                }else{
                        imagecopyresampled($dest_img, $src_img, 0, 0, 0, 0, $w, $h, $size_img[0], $size_img[1]);
                }
        
                imagejpeg($dest_img, $smallimage, $q);
                imagedestroy($dest_img);
                imagedestroy($src_img);
                imagedestroy($tmp_image);
                return true;
    }
    
    public function copy($image, $pref = null)
    {
        $uploadfile =  time() . '.jpeg';
        if (@copy($image, $this->_fullPath . $uploadfile)) {
            $i=0;
            if($pref !== null){
            			if(!file_exists($this->_fullPath.$pref.'/'))
                    	{
                    		mkdir($this->_fullPath.$pref.'/');
                    	}
            	$this->resizeimg($this->_fullPath . $uploadfile, 
            			         $this->_fullPath. $pref . '/'. $uploadfile,
            			         $this->prefixs[$pref][0],
            			         $this->prefixs[$pref][1],
            			         95);
            } else {
                    foreach($this->prefixs as $prefix => $size)
                    {
                        if(!file_exists($this->_fullPath.$prefix.'/'))
                        {
                            mkdir($this->_fullPath.$prefix.'/', 0777);
                        }
                        $this->resizeimg($this->_fullPath . $uploadfile, $this->_fullPath. $prefix . '/'. $uploadfile,$size[0],$size[1],95);
                    $i++;
                    }
                @unlink($this->_fullPath . $uploadfile);
            	@unlink($image);       	
            }

             return true;
        }
    }
}
?>